﻿using System;
using System.Linq;
using System.Reflection;
using Styx.Helpers;
using Styx.Plugins;
using System.Collections.Generic;
using Styx.WoWInternals;

namespace PetFighterBot
{
    public class PokehbuddygApi
    {
        private readonly PluginContainer _PokehbuddyCtn;
        private readonly Action _pulse;
        private readonly Action _OnButtonPress;
        
        public PokehbuddygApi()
        {
            // find the Pokehbuddy plugin assembly
            _PokehbuddyCtn = PluginManager.Plugins.FirstOrDefault(
                a => a.Name.Contains("Pokébuddy"));
            if (_PokehbuddyCtn != null)
            {
                if (_PokehbuddyCtn.Enabled)
                    _PokehbuddyCtn.Enabled = false;

                // Pulse
                _pulse = _PokehbuddyCtn.Plugin.Pulse;
                // OnButtonPress
                _OnButtonPress = _PokehbuddyCtn.Plugin.OnButtonPress;
            }
        }

        /// <summary>
        /// </summary>
        public bool HasPokehbuddy()
        {
            return _PokehbuddyCtn != null ? true : false;
        }
        
        /// <summary>
        /// </summary>
        public void Pulse()
        {
            if (_pulse != null) _pulse();
        }

        public void OnButtonPress()
        {
            if (_OnButtonPress != null) _OnButtonPress();
        }



        public int GetPetHPPreCombat(int petnum)
        {

            int getal = 0;
            List<string> cnt = Lua.GetReturnValues("local petID= C_PetJournal.GetPetLoadOutInfo(" + petnum + ") local health, maxHealth, attack, speed, rarity = C_PetJournal.GetPetStats(petID) local dummy = (health / maxHealth) * 100 return dummy");
            try
            {
                cnt[0].Replace(",", ".");
                int i = cnt[0].IndexOf('.');

                // Remainder of string starting at 'c'.
                if (i > -1) cnt[0] = cnt[0].Substring(0, i);
                //BBLog("Lua received HP :"+cnt[0]);
                getal = Convert.ToInt32(cnt[0]);
            }
            catch (Exception exc)
            {

            }


            return getal;
        }

        public string ReadActiveSlot()
        {
            List<string> cnt = Lua.GetReturnValues("dummy={} for i = 1, 3   do  local petID= C_PetJournal.GetPetLoadOutInfo(i); dummy[i]=petID  end return tonumber(dummy[C_PetBattles.GetActivePet(1)],16);");
            //Logging.Write(cnt[0]);
            //int decAgain = int.Parse(, System.Globalization.NumberStyles.HexNumber);
            return cnt[0];//decAgain.ToString();

        }

        public int GetLowLevel()
        {
            int getal = 0;
            List<string> cnt = Lua.GetReturnValues("local dummy = 99 for j=1,3 do local petID= C_PetJournal.GetPetLoadOutInfo(j) local speciesID, customName, level = C_PetJournal.GetPetInfoByPetID(petID) if level < dummy then dummy=level end end return dummy");
            try
            {
                //BBLog(""+cnt[0]);
                getal = Convert.ToInt32(cnt[0]);
            }
            catch (Exception exc)
            {

            }


            return getal;
        }
        public int GetHighLevel()
        {
            int getal = 0;
            List<string> cnt = Lua.GetReturnValues("local dummy = 0 for j=1,3 do local petID= C_PetJournal.GetPetLoadOutInfo(j) local speciesID, customName, level = C_PetJournal.GetPetInfoByPetID(petID) if level > dummy then dummy=level end end return dummy");
            try
            {
                //BBLog(""+cnt[0]);
                getal = Convert.ToInt32(cnt[0]);
            }
            catch (Exception exc)
            {

            }


            return getal;
        }

        public int GetWildLevel()
        {
            //Logging.Write(GUID);
            int getal = 0;
            List<string> cnt = Lua.GetReturnValues("return UnitBattlePetLevel('target')");
            try
            {
                //BBLog(""+cnt[0]);
                getal = Convert.ToInt32(cnt[0]);
            }
            catch (Exception exc)
            {

            }


            return getal;
        }

    }

    public class BBSettings : Settings
    {

        public BBSettings(string settingsPath)
            : base(settingsPath)
        {
            Load();
        }

        [Setting, DefaultValue(1)]
        public int HPFactor { get; set; }

        [Setting, DefaultValue(1)]
        public int LevelFactor { get; set; }

        [Setting, DefaultValue(1)]
        public int AdFactor { get; set; }

        [Setting, DefaultValue(1)]
        public int DisFactor { get; set; }

        [Setting, DefaultValue(50)]
        public int Distance { get; set; }

        [Setting, DefaultValue(3)]
        public int GetRarity { get; set; }

        [Setting, DefaultValue(5)]
        public int BlacklistCounterLimit { get; set; }

        [Setting, DefaultValue(5)]
        public int SkipCounterLimit { get; set; }

        [Setting, DefaultValue(false)]
        public bool DoPVP { get; set; }

        [Setting, DefaultValue(5)]
        public int PVPMinTime { get; set; }

        [Setting, DefaultValue(10)]
        public int PVPMaxTime { get; set; }

        [Setting, DefaultValue(3)]
        public int BelowLevel { get; set; }

        [Setting, DefaultValue(3)]
        public int AboveLevel { get; set; }

        [Setting, DefaultValue(false)]
        public bool UseBandages { get; set; }

        [Setting, DefaultValue(false)]
        public bool ForfeitIfNotInteresting { get; set; }


        [Setting, DefaultValue(true)]
        public bool MailEveryReset { get; set; }

        [Setting, DefaultValue(true)]
        public bool AmHorde { get; set; }

        [Setting, DefaultValue(false)]
        public bool IBSupport { get; set; }

    }
}